<template>
	<view class="call-dialog" v-if="showDialog">
		<view class="dialog-content">
			<view class="close" @click='closeDialog'>
				<uni-icons type="closeempty" color="#999999" size="24"></uni-icons>

			</view>
			<text class="title">即将拨打该号码</text>
			<view class="phone-number-box">
				<text class="phone-number">{{ phoneNumber }}</text>
			</view>
			<button class="call-btn" @click="handleCall">立即呼叫（加密）</button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		defineProps,
		defineEmits
	} from 'vue';
	// 定义接收的 props，用于传入电话号码和控制显示隐藏
	const props = defineProps({
		showDialog: Boolean,
		phoneNumber: String
	});
	// 定义触发的事件，比如点击呼叫后的回调
	const emit = defineEmits(['call']);

	const handleCall = () => {
		// 校验电话号码是否有效
		if (!props.phoneNumber) {
			uni.showToast({
				title: '电话号码为空',
				icon: 'none'
			});
			return;
		}

		// 调用UniApp拨打电话API
		uni.makePhoneCall({
			phoneNumber: props.phoneNumber,
			success: () => {
				console.log('拨打电话成功');
				// 可在此处添加呼叫成功后的逻辑（如关闭弹窗）
				emit('call'); // 触发父组件事件
				emit('close');
			},
			fail: (err) => {
				console.error('拨打电话失败', err);
				uni.showToast({
					title: '拨打失败，请检查号码或重试',
					icon: 'none'
				});
			}
		});
	};
	// 添加关闭弹窗的方法
	const closeDialog = () => {
		emit('close'); // 触发父组件的 close 事件
	};
</script>

<style scoped lang="scss">
	.call-dialog {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.3);
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 99;
	}

	.dialog-content {
		background-color: #fff;
		padding: 30rpx;
		border-radius: 20rpx;
		width: 80%;
		text-align: center;
		height: 459rpx;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		justify-content: space-between;

		.close {
			display: flex;
			justify-content: flex-end;
		}

	}

	.title {
		font-size: 30rpx;
		font-weight: 500;
		display: block;
		color: #303133;
	}

	.phone-number-box {
		background: #F6F6F6;
		height: 166rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 16rpx;

		.phone-number {
			font-size: 40rpx;
			color: #303133;
			font-weight: bold;
		}
	}


	.call-btn {
		width: 100%;
		background: #2F80ED;
		color: #fff;
		font-size: 28rpx;
		padding: 20rpx 40rpx;
		border-radius: 34rpx;
		height: 68rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>